package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.mysql.jdbc.PreparedStatement;

import dto.TeacherDto;

public class TeacherDao {

	public String insertTeacher(TeacherDto teacher) {

		if (teacher == null)
			return "Insert failed! Input data is null.";
		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "INSERT INTO teacher " + "(user_id, password) "
				+ "VALUES (?,?)";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getId());
			preparedStatement.setString(2, teacher.getPassword());
			// preparedStatement.setInt(3, teacher.getUserId());

			preparedStatement.execute();
			response = "Inserted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Insert failed! " + e.getMessage();
		}

		return response;
	}

	public String deleteTeacher(String teacherId) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "DELETE FROM teacher WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, teacherId);

			preparedStatement.execute();
			response = "Deleted successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Delete failed! " + e.getMessage();
		}

		return response;
	}

	public String updateTeacher(TeacherDto teacher) {

		String response = "";

		Connection connection = DataProvider.getConnection();
		String sql = "UPDATE Teacher SET password = ? , user_user_id = ?"
				+ " WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);

			preparedStatement.setString(1, teacher.getPassword());
			preparedStatement.setInt(2, teacher.getUserId());
			preparedStatement.setString(3, teacher.getId());

			preparedStatement.execute();
			response = "Updated successfully!";

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			response = "Update failed! " + e.getMessage();
		}

		return response;
	}

	public TeacherDto getTeacherById(String teacherId) {
		TeacherDto teacher = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM Teacher WHERE user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setString(1, teacherId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				teacher = new TeacherDto();
				teacher.setId(resultSet.getString("user_id"));
				teacher.setPassword(resultSet.getString("password"));
				teacher.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return teacher;
	}

	public TeacherDto getTeacherByUserId(int userId) {
		TeacherDto teacher = null;

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM Teacher WHERE user_user_id = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(sql);
			preparedStatement.setInt(1, userId);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				teacher = new TeacherDto();
				teacher.setId(resultSet.getString("user_id"));
				teacher.setPassword(resultSet.getString("password"));
				teacher.setUserId(resultSet.getInt("user_user_id"));
			}

			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return teacher;
	}

	public ArrayList<TeacherDto> getAllTeacher() {

		ArrayList<TeacherDto> listTeacher = new ArrayList<>();

		Connection connection = DataProvider.getConnection();
		String sql = "SELECT * FROM Teacher";

		Statement statement;
		try {
			statement = connection.createStatement();

			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				TeacherDto teacher = new TeacherDto();
				teacher.setId(resultSet.getString("user_id"));
				teacher.setPassword(resultSet.getString("password"));
				teacher.setUserId(resultSet.getInt("user_user_id"));
				listTeacher.add(teacher);
			}

			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return listTeacher;
	}

}
